﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace SQLServerDAL
{

    public class RechargeLogDAL : IRechargeLogDAL
    {
         public Boolean InsertRechargeLog(RechargeLog rechargeLog) 
        {
            try
            {
                
                RechargeLogDataClassesDataContext rechargeLogContext = new RechargeLogDataClassesDataContext();
                rechargeLogContext.RechargeLogs.InsertOnSubmit(rechargeLog);
                rechargeLogContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteRechargeLog(int rechargeLogId) 
        {
            try
            {
                RechargeLogDataClassesDataContext rechargeLogContext = new RechargeLogDataClassesDataContext();
                RechargeLog rechargeLog = (from u in rechargeLogContext.RechargeLogs
                             where u.RechargeLogId.Equals(rechargeLogId)
                             select u).First();
                rechargeLogContext.RechargeLogs.DeleteOnSubmit(rechargeLog);
                rechargeLogContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateRechargeLog(int rechargeLogId,RechargeLog rechargeLog) 
        {
            
                if (rechargeLogId != rechargeLog.RechargeLogId) {
                    return false;
                }
                RechargeLogDataClassesDataContext rechargeLogContext = new RechargeLogDataClassesDataContext();
                RechargeLog query = (from u in rechargeLogContext.RechargeLogs
                             where u.RechargeLogId.Equals(rechargeLogId)
                             select u).First();
                //query.Password = rechargeLog.Password;
                
                rechargeLogContext.SubmitChanges();
                return true;
            
        }

        public RechargeLog SelectRechargeLogById(int rechargeLogId)
        {
            RechargeLogDataClassesDataContext rechargeLogContext = new RechargeLogDataClassesDataContext();
            RechargeLog rechargeLog = (from u in rechargeLogContext.RechargeLogs
                         where u.RechargeLogId.Equals(rechargeLogId)
                         select u).First();
            return rechargeLog;
        }


        public List<RechargeLog> SelectAllRechargeLog() 
        {
            RechargeLogDataClassesDataContext rechargeLogContext = new RechargeLogDataClassesDataContext();
            var query = (from u in rechargeLogContext.RechargeLogs
                         select u);
            List<RechargeLog> rechargeLogList = new List<RechargeLog>();

            foreach (var rechargeLog in query) {
                rechargeLogList.Add(rechargeLog);
            }
            return rechargeLogList;
        }

    }
}
